package com.xyzyzglxt.web.controller.volunteer;

import com.xyzyzglxt.common.annotation.Log;
import com.xyzyzglxt.common.core.controller.BaseController;
import com.xyzyzglxt.common.core.domain.AjaxResult;
import com.xyzyzglxt.common.enums.BusinessType;
import com.xyzyzglxt.common.utils.FastDFSUtils;
import com.xyzyzglxt.system.service.ISysEventService;
import com.xyzyzglxt.system.service.ISysUserEventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.javamail.ConfigurableMimeFileTypeMap;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import redis.clients.jedis.JedisCluster;


@Api(tags = "活动照片管理")
@RestController
@RequestMapping("/volunteer/image")
public class ImgController extends BaseController {

    @Autowired
    private ISysEventService sysEventService;
    @Autowired
    private ISysUserEventService sysUserEventService;

    /**
     * 上传(更新)活动图片
     * @return
     */
    @ApiOperation("上传(更新)活动图片")
    @Log(title = "上传(更新)活动图片", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('volunteer:event:image')")
    @PostMapping("/upload")
    public AjaxResult uploadImage(MultipartFile file,Long eventId) {
        String[] filePath = FastDFSUtils.upload(file);
        String url = FastDFSUtils.getTrackerUrl()+filePath[0]+"/"+filePath[1];
        return toAjax(sysEventService.updateSysEventImg(url,eventId));
    }

    /**
     * 上传自愿者服务时的证明图片
     * @param file
     * @return
     */
    @ApiOperation("上传(更新)自愿者服务时的证明图片")
    @PreAuthorize("@ss.hasPermi('volunteer:user:image')")
    @Log(title = "上传(更新)自愿者服务时的证明图片", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadUserEvent")
    public AjaxResult uploadImage(MultipartFile file,Long eventId,Long userId) {
        String[] filePath = FastDFSUtils.upload(file);
        String url = FastDFSUtils.getTrackerUrl()+filePath[0]+"/"+filePath[1];
        int num = sysUserEventService.updateSysEventImg(url,eventId,userId);
        if(num == -1){
            return error("仅能上传自己的活动照片");
        }
        return success();
    }

}
